package com.feituan.manager.controller;

import com.feituan.core.persistence.service.IIntegrationRecordService;
import com.feituan.core.pojo.entity.IntegrationRecord;
import com.feituan.core.pojo.request.IntegrationRecordReq;
import com.feituan.core.pojo.response.IntegrationRecordRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/integrationRecord")
public class IntegrationRecordController {

    @Resource
    private IIntegrationRecordService integrationRecordService;


    /**
     * 新增订单
     * @param recordReq recordReq
     */
    @PostMapping("/save")
    public Response<String> saveRecord(IntegrationRecordReq recordReq) {
        if (recordReq.getId() == null) {
            String res = integrationRecordService.saveRecord(recordReq);
            if ("false".equals(res)) {
                return Response.success("", "兑换失败");
            }
            return Response.success(res);

        } else {
            int res = integrationRecordService.updateRecord(recordReq);
            if (res > 0) {
                return Response.success("true");
            } else {
                return Response.success("false");
            }
        }

    }


    /**
     * 订单发货
     * @param recordReq recordReq
     * @return
     */
    @PostMapping("/send")
    public Response<Boolean> sendOrder(IntegrationRecordReq recordReq) {
        return Response.success(integrationRecordService.sendOrder(recordReq) > 0);
    }


    /**
     * 查询列表
     * @param recordReq recordReq
     * @return
     */
    @PostMapping("/list_page")
    public Response<PageData<IntegrationRecordRes>>  selectList(IntegrationRecordReq recordReq) {
        return Response.success(integrationRecordService.selectList(recordReq));
    }


    /**
     * 根据人员id查询订单
     * @param userId userId
     * @return
     */
    @PostMapping("/byUserId")
    public Response<List<IntegrationRecordRes>> selectListByUserId(Long userId) {
        return Response.success(integrationRecordService.selectListByUserId(userId));
    }


    /**
     * 通过订单号获取详情
     * @param number
     * @return
     */
    @PostMapping("/getByNumber")
    public Response<IntegrationRecordRes> selectByNumber(String number) {
        return Response.success(integrationRecordService.selectByNumber(number));
    }


}
